<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Controller;
use App\Kuafu\Pay\Model\PayCallbackLog;
use App\Kuafu\Pay\Service\PayService;
use Closure;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Route;

class PayCallbackAfter extends Controller
{
    /**
     * 支付回调的after middleware
     * @param $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (PayService::$callBackId) {
            $responseData = $response->getContent();
            if (!is_string($responseData)) {
                $responseData = json_encode($responseData);
            }

            PayCallbackLog::editResponseData(PayService::$callBackId, $responseData);
        }

        return $response;
    }
}
